﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInfo;

namespace WebAdmin.Controllers
{
    [Authorize]
    public class NewsCataController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllNewsCataByPage(StoreRequestParameters parms)
        {
            WI_NewsCataDAL ndal = new WI_NewsCataDAL();
            int ttlCount;
            List<NewsCata> catalist = ndal.GetAllUINewsCata(parms.Start, parms.Limit, out ttlCount);
            return this.Store(catalist, ttlCount);
        }

        public ActionResult GetAllNewsCata(int? cataId)
        {
            WI_NewsCataDAL ndal = new WI_NewsCataDAL();
            List<WI_NewsCata> dbcatalist = ndal.GetAllNewsCata();
            if (cataId.HasValue)
            {
                WI_NewsCata wicata = dbcatalist.SingleOrDefault(c => c.CataID == cataId);
                if (wicata != null)
                {
                    dbcatalist.Remove(wicata);
                }
            }
            List<NewsCata> catalist = new List<NewsCata>();
            foreach (var cata in dbcatalist)
            {
                //这里不需要显示RootCataName和ParentCataName，所以赋值为空
                NewsCata ncata = NewsCata.AsNewsCata(cata, "", "");
                catalist.Add(ncata);
            }
            return this.Store(catalist);
        }

        [HttpPost]
        public ActionResult DeleteNewsCata(List<NewsCata> delCatas)
        {
            try
            {
                WI_NewsCataDAL ndal = new WI_NewsCataDAL();
                foreach (var ncata in delCatas)
                {
                    ndal.RemoveNewsCata(ncata.CataID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewNewsCataWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建目录",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateNewsCata"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateNewsCata()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateNewsCata(NewsCata ncata)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                string creator = Request.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
                ncata.Creator = creator;
                WI_NewsCataDAL ndal = new WI_NewsCataDAL();
                ndal.CreateNewsCata(ncata);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "目录创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditNewsCataWindow(int cataId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑目录",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditNewsCata"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("cataId", cataId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditNewsCata(int cataId)
        {
            WI_NewsCataDAL ndal = new WI_NewsCataDAL();
            WI_NewsCata wicata = ndal.GetNewsCata(cataId);
            //这里不需要显示RootCataName和ParentCataName，所以赋值为空
            NewsCata ncata = NewsCata.AsNewsCata(wicata, "", "");
            return View(ncata);
        }

        [HttpPost]
        public ActionResult EditNewsCata(NewsCata ncata)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_NewsCataDAL ndal = new WI_NewsCataDAL();
                ndal.UpdateNewsCata(ncata.CataID, ncata);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "目录更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
